//
//  VideoTableViewCell.m
//  CYWFrame
//
//  Created by Macx on 2017/9/25.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "VideoTableViewCell.h"

#import "Articles.h"
#import "PushPerHome.h"

@implementation VideoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.title.font = kUHSystemFontWithSize(16);
    self.title.numberOfLines = 2;
    self.name.font = kUHSystemFontWithSize(12);
    self.headPic.frame = CGRectMake(10, 10, SW(10), SW(10));
    self.headPic.layer.cornerRadius = self.headPic.width/2;
    self.headPic.layer.masksToBounds = YES;
    
    
    CGFloat videoW = SCREEN_WIDTH-2*self.headPic.x;
    self.videoPic.frame = CGRectMake(self.headPic.x, 10, videoW, videoW*9/16);
    self.videoPic.contentMode = UIViewContentModeScaleAspectFill;
    self.videoPic.clipsToBounds = YES;
    self.videoPlayIcon.size = CGSizeMake(self.videoPic.height/4, self.videoPic.height/4);
    self.line.backgroundColor = UISeparatedColorRGB;
    
    [self.moneyBtn setImage:[IMAGE(@"icon_reward") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.moneyBtn setTitle:@" 打赏" forState:UIControlStateNormal];
    self.moneyBtn.titleLabel.font = kUHSystemFontWithSize(12);
    int state = [LZCTools thinkEnv];
    if (state == 0) {
        self.moneyBtn.hidden = YES;
    }
    
    
    [self.praiseBtn setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.praiseBtn setTitle:@" 110" forState:UIControlStateNormal];
    self.praiseBtn.titleLabel.font = kUHSystemFontWithSize(12);
    
    [self.commentBtn setImage:[IMAGE(@"icon_comment") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.commentBtn setTitle:@" 110" forState:UIControlStateNormal];
    self.commentBtn.titleLabel.font = kUHSystemFontWithSize(12);
    
    [self.shareBtn setImage:[IMAGE(@"placeHolder") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

    
}

- (void)setModel:(Articles *)model {
    if (_model != model) {
        _model = model;
        
//        self.title.text = model.title;
//        NSMutableAttributedString *lsStr = [[NSMutableAttributedString alloc] initWithString:self.title.text];
//        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//        [paragraphStyle setLineSpacing:4];//调整行间距
//        [lsStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.title.text length])];
//        self.title.attributedText = lsStr;
        
        CGSize size1 = [LZCTools textSizeWithText:@"字数" font:self.title.font];
//        if (model.istop == 0) {
////            self.title.text = [NSString stringWithFormat:@"[置顶]%@", model.title];
////            NSMutableAttributedString *top = [[NSMutableAttributedString alloc] initWithString:self.title.text];
////            NSRange redRang = NSMakeRange([[top string] rangeOfString:@"[置顶]"].location, [[top string] rangeOfString:@"[置顶]"].length);
////            [top addAttribute:NSForegroundColorAttributeName value:kRedColor range:redRang];
////            [self.title setAttributedText:top];
//
//            // 创建一个富文本
//            NSMutableAttributedString *hintString2 = [[NSMutableAttributedString alloc] initWithString:model.title];
//            //获取要调整颜色的文字位置,调整颜色
//            NSRange range2=[[hintString2 string]rangeOfString:model.title];
//            [hintString2 addAttribute:NSForegroundColorAttributeName value:kBlackColor range:range2];
//
//            CGSize coinSize = [LZCTools textSizeWithText:@"测 试" font:kUHSystemFontWithSize(13)];
//            // 添加表情
//            NSTextAttachment *attch = [[NSTextAttachment alloc] init];
//            // 表情图片
//            attch.image = [UIImage imageNamed:@"icon_set_top"];
//            // 设置图片大小
//            attch.bounds = CGRectMake(0, -(size1.height-coinSize.height)/2, coinSize.width, coinSize.height);
//            // 创建带有图片的富文本
//            NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
//            NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithAttributedString:string];
//            [string2 appendAttributedString:hintString2];
//            [self.title setAttributedText:string2];
//        }
//        else {
            self.title.text = model.title;
//        }
        
        
        self.name.text = [model.author valueForKey:@"nickname"];
        [self.headPic sd_setImageWithURL:[NSURL URLWithString:[model.author valueForKey:@"face"]] placeholderImage:IMAGE(@"placeHolder")];
        
        [self.headPic addActionWithblock:^{
            [PushPerHome pushPerHomeWithUserInfo:[model valueForKey:@"author"]];
        }];
        
        
        if ([[model.author valueForKey:@"role"] isEqualToString:@"1"]) {
            self.role.image = IMAGE(@"");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"2"]) {
            self.role.image = IMAGE(@"icon_add_v");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"3"]) {
            self.role.image = IMAGE(@"icon_add_v_expert");
        }

        
        [self.videoPic sd_setImageWithURL:[NSURL URLWithString:model.cover_url] placeholderImage:IMAGE(@"placeHolder")];
        
        if (model.is_rewarded == 1) {
            [self.moneyBtn setTitle:@"已打赏" forState:UIControlStateNormal];
            [self.moneyBtn setTitleColor:kRedColor forState:UIControlStateNormal];
        }
        else {
            [self.moneyBtn setTitle:@"打赏" forState:UIControlStateNormal];
            [self.moneyBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        }

        
        if (model.is_zan == 1) {
            [self.praiseBtn setImage:[IMAGE(@"index_zan_ed") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        }
        else
            [self.praiseBtn setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self.praiseBtn setTitle:[NSString stringWithFormat:@" %ld", model.zan] forState:UIControlStateNormal];
        
        [self.commentBtn setTitle:[NSString stringWithFormat:@" %ld", model.comments_count] forState:UIControlStateNormal];
        
        CGSize titleSize = [self.title.text sizeWithFont:kUHSystemFontWithSize(16) constrainedToSize:CGSizeMake(SCREEN_WIDTH-20, size1.height*10) lineBreakMode:NSLineBreakByWordWrapping];
        if (size1.height*2 < titleSize.height) {
            self.title.frame = CGRectMake(10, 10, SCREEN_WIDTH-20, size1.height*2);
        }
        else
            self.title.frame = CGRectMake(10, 10, SCREEN_WIDTH-20, titleSize.height);

        self.videoPic.y = self.title.y+self.title.height+10;
        self.videoPlayIcon.center = CGPointMake(self.videoPic.centerX, self.videoPic.centerY);
        self.headPic.y = self.videoPic.y+self.videoPic.height+10;
        self.name.frame = CGRectMake(self.headPic.x+self.headPic.width+5, self.headPic.y, [LZCTools textSizeWithText:self.name.text font:self.name.font].width, self.headPic.height);
        
        
        self.role.frame = CGRectMake(self.headPic.x+self.headPic.width/3*2, self.headPic.y+self.headPic.height/3*2, self.headPic.width/3, self.headPic.width/3);
        
        CGFloat btnH = self.headPic.height;
        CGFloat mW = [LZCTools textSizeWithText:@"打赏打赏" font:self.moneyBtn.titleLabel.font].width;
//        self.shareBtn.frame = CGRectMake(SCREEN_WIDTH-10-btnH, self.headPic.y, btnH, btnH);
        self.commentBtn.frame = CGRectMake(SCREEN_WIDTH-10-btnH, self.headPic.y, btnH+10, btnH);
        self.praiseBtn.frame = CGRectMake(self.commentBtn.x-10-btnH, self.headPic.y, btnH+10, btnH);
        self.moneyBtn.frame = CGRectMake(self.praiseBtn.x-10-mW, self.headPic.y, mW+10, btnH);
        
        self.line.frame = CGRectMake(0, self.headPic.y+self.headPic.height+10, SCREEN_WIDTH, 10);
        
        self.height = self.line.y+self.line.height;
        
    }
}


- (CGFloat)cellHeight {
    
    //    return self.sepView.y+self.sepView.height;
    return self.height;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
